package com.bawei.login.view

import android.view.View
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.common.RouterPath
import com.bawei.common.mvi.BaseMviActivity
import com.bawei.login.R
import com.bawei.login.databinding.ActivityLoginBinding
import com.bawei.login.entity.LoginValues
import com.bawei.login.intent.LoginViewIntent
import com.bawei.login.uistate.LoginUiState
import com.bawei.login.viewmodel.LoginViewModel
import com.tencent.mmkv.MMKV
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@Route(path = RouterPath.PATH_LOGIN)
@AndroidEntryPoint
class LoginActivity : BaseMviActivity<ActivityLoginBinding>() {

    private val mViewModel: LoginViewModel by viewModels()


    override fun getLayoutId(): Int = R.layout.activity_login

    override fun initViews() {
        //获取记录的账号和密码
        var preAccount = MMKV.defaultMMKV().decodeString("pre_account","")
        var prePwd = MMKV.defaultMMKV().decodeString("pre_pwd","")
        if (!preAccount.isNullOrEmpty() && !prePwd.isNullOrEmpty()){
            mDataBinding.includeLogin.cbRememberPwd.isChecked = true
            mDataBinding.includeLogin.edtLoginName.setText(preAccount)
            mDataBinding.includeLogin.edtLoginPwd.setText(prePwd)
        }

        //注册按钮事件
        mDataBinding.includeLogin.tvRegister.setOnClickListener {
            showRegisterCardView()
        }

        //登录按钮点击事件
        mDataBinding.includeLogin.btnLogin.setOnClickListener {
            var uName = mDataBinding.includeLogin.edtLoginName.text.toString().trim()
            var uPwd = mDataBinding.includeLogin.edtLoginPwd.text.toString().trim()
            if (!mDataBinding.cbAgree.isChecked){
                showMessage("请先同意用户协议")
            } else {
                loginToServer(uName, uPwd)
            }
        }

        //注册按钮点击事件
        mDataBinding.includeRegister.btnRegister.setOnClickListener {
            var uName = mDataBinding.includeRegister.edtRegisterName.text.toString().trim()
            var uPwd = mDataBinding.includeRegister.edtRegisterPwd.text.toString().trim()
            var uRePwd = mDataBinding.includeRegister.edtRegisteRePwd.text.toString().trim()
            if (!uName.isNullOrEmpty() && !uPwd.isNullOrEmpty() && !uRePwd.isNullOrEmpty() && uPwd == uRePwd){
                registerToServer(uName, uPwd)
            }
        }
    }

    /**
     * 显示登录布局卡片
     */
    private fun showLoginCardView(){
        //隐藏登录布局卡片
        mDataBinding.cltLoginCard.visibility = View.VISIBLE
        //显示注册布局卡片
        mDataBinding.cltRegisterCard.visibility = View.INVISIBLE
        //隐藏用户协议
        mDataBinding.llUserAgreement.visibility = View.VISIBLE
    }

    /**
     *  显示注册布局卡片
     */
    private fun showRegisterCardView(){
        //隐藏登录布局卡片
        mDataBinding.cltLoginCard.visibility = View.INVISIBLE
        //显示注册布局卡片
        mDataBinding.cltRegisterCard.visibility = View.VISIBLE
        //隐藏用户协议
        mDataBinding.llUserAgreement.visibility = View.INVISIBLE
    }

    /**
     * 登录到服务器
     */
    private fun loginToServer(uName: String = "zxy", uPwd: String = "123456"){
        lifecycleScope.launch {
            mViewModel.channel.send(LoginViewIntent.LoginToServer(uName, uPwd))
        }
    }

    /**
     * 注册到服务器
     */
    private fun registerToServer(uName: String, uPwd: String){
        lifecycleScope.launch {
            mViewModel.channel.send(LoginViewIntent.RegisterToServer(uName, uPwd))
        }
    }

    override suspend fun collectUiState() {
        mViewModel.uiState.collect{
            when(it){
                is LoginUiState.LoginSuccess -> handLoginSuccess(it.data)
                is LoginUiState.RegisterSuccess -> handRegisterSuccess(it.data)
                is LoginUiState.LoginFail -> showMessage(it.msg)
                is LoginUiState.RegisterFail -> showMessage(it.msg)
                else -> {}
            }
        }
    }

    private fun handLoginSuccess(data: Any) {
        var entity = data as LoginValues
        //判断是否选中记录密码
        if(mDataBinding.includeLogin.cbRememberPwd.isChecked){
            MMKV.defaultMMKV().encode("pre_account", entity.uName)
            MMKV.defaultMMKV().encode("pre_pwd", entity.uPwd)
        }
        //跳转到主页面
        ARouter.getInstance().build(RouterPath.PATH_MAIN).navigation()
        //关闭当前页面
        finish()
    }

    private fun handRegisterSuccess(data: Any) {
        var entity = data as LoginValues
        //回显到登录页面输入框
        mDataBinding.includeLogin.edtLoginName.setText(entity.uName)
        mDataBinding.includeLogin.edtLoginPwd.setText(entity.uPwd)
        //提示一下
        showMessage("注册成功！")
        showLoginCardView()
    }


}